﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using UNDP_Projects.Data_Access;

namespace UNDP_Projects.Project
{
    public partial class Add_Report : System.Web.UI.Page
    {
        protected void Page_PreInit(object sender, EventArgs e)
        {
            /*if (User.IsInRole("Project"))
            {
                MasterPageFile = "~/MasterPages/Project.Master";
            }
            if (User.IsInRole("Programme"))
            {
                MasterPageFile = "~/MasterPages/Programme.Master";
            }
            if (User.IsInRole("Administrator"))
            {
                MasterPageFile = "~/MasterPages/CO.Master";
            }
            if (User.IsInRole("Project") && User.IsInRole("Programme"))
            {
                MasterPageFile = "~/MasterPages/ProgProject.Master";
            }*/
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            
        }

        protected void ReportButton_Click(object sender, EventArgs e)
        {
            
            //Response.Redirect("~/Admin/Delete.aspx");

            using (UNDPEntities db = new UNDPEntities())
            {
                try
                {
                    int projID = int.Parse(ProjectList.SelectedValue);
                    int reportPeriodID = int.Parse(ReportPeriodList.SelectedValue);
                    int yearID = int.Parse(YearList.SelectedValue);

                    var myReport = (from report in db.project_reports 
                                        where report.project_id == projID && 
                                        report.year_id == yearID && 
                                        report.report_period_id == reportPeriodID 
                                        select report).FirstOrDefault();

                    if (myReport == null)
                    {
                        if (!User.IsInRole("Administrator"))
                        {
//======================================================================================================
                            //char[] separator = new char[] { '\\' };
                            //string[] uName = User.Identity.Name.Split(separator);
                            //string uname2 = uName[1] + "@undp.org";
                            //var UProject = (from u in db.users where u.user_email == uname2 select u.project_id).Single();
//=======================================================================================================
                            var UProject = (from u in db.users where u.user_email == User.Identity.Name select u.project_id).Single();
                            if (UProject == null)
                            {
                                Error.Text = "You cannot create this project";
                                Error.Visible = true;
                                Error.ForeColor = System.Drawing.Color.Red;
                                return;
                            }

                            string[] theProj = UProject.Split(new Char[]{','});

                            foreach (var UProjAccess in theProj)
                            {
                                try
                                {
                                    long projToAccess = long.Parse(UProjAccess);

                                    if (projToAccess == projID)
                                    {
                                        //======================================+
                                        //  Add a new Project_Report Record     |
                                        //======================================+
                                        project_reports pr = new project_reports();
                                        pr.project_id = projID;
                                        pr.year_id = yearID;
                                        pr.report_period_id = reportPeriodID;
                                        pr.project_status_id = 1;

                                        string donor = "";
                                        foreach (ListItem li in DonorListBox.Items)
                                        {
                                            if (li.Selected == true)
                                            {
                                                donor += li.Text + "-";
                                            }
                                        }

                                        pr.donors = donor;
                                        pr.project_editor = EditorTextBox.Text;
                                        pr.saved = true;
                                        pr.saved_date = DateTime.Now.ToString();

                                        db.project_reports.AddObject(pr);
                                        db.SaveChanges();

                                        //=================================+
                                        //  Add a new Log Track Record     |
                                        //=================================+
                                        log_track newTrack = new log_track();
                                        newTrack.project_id = projID;
                                        newTrack.year_id = yearID;
                                        newTrack.report_period_id = reportPeriodID;
                                        newTrack.log_track1 += DateTime.Now.ToString() + ": created by :" + User.Identity.Name + ",";
                                        db.log_track.AddObject(newTrack);
                                        db.SaveChanges();

                                        Response.Redirect("~/Project/Saved_Projects/", true);
                                    }
                                    else
                                    {
                                        Error.Text = "You cannot create this project";
                                        Error.Visible = true;
                                        Error.ForeColor = System.Drawing.Color.Red;
                                    }
                                }
                                catch
                                {
                                    Error.Text = "You cannot create this project";
                                    Error.Visible = true;
                                    Error.ForeColor = System.Drawing.Color.Red;
                                }
                            }
                        }
                        else
                        {
                            //======================================+
                            //  Add a new Project_Report Record     |
                            //======================================+
                            project_reports pr = new project_reports();
                            pr.project_id = projID;
                            pr.year_id = yearID;
                            pr.report_period_id = reportPeriodID;
                            pr.project_status_id = 1;

                            string donor = "";
                            foreach (ListItem li in DonorListBox.Items)
                            {
                                if (li.Selected == true)
                                {
                                    donor += li.Text + "-";
                                }
                            }

                            pr.donors = donor;
                            pr.project_editor = EditorTextBox.Text;
                            pr.saved = true;
                            pr.saved_date = DateTime.Now.ToString();

                            db.project_reports.AddObject(pr);
                            db.SaveChanges();

                            //=================================+
                            //  Add a new Log Track Record     |
                            //=================================+
                            log_track newTrack = new log_track();
                            newTrack.project_id = projID;
                            newTrack.year_id = yearID;
                            newTrack.report_period_id = reportPeriodID;
                            newTrack.log_track1 += DateTime.Now.ToString() + ": created by :" + User.Identity.Name + ",";
                            db.log_track.AddObject(newTrack);
                            db.SaveChanges();

                            Response.Redirect("~/Project/Saved_Projects/", true);
                        }
                    }
                    else
                    {
                        Error.Text = "That report has already been added to the database," +
                                " Please look in the saved projects page";
                        Error.Visible = true;
                        Error.ForeColor = System.Drawing.Color.Red;
                        Response.Redirect("~/Project/Saved_Projects/", true);
                    }
                }
                catch (Exception ex)
                {
                    Error.Text = "Database error : " + ex.Message + " : " + ex.StackTrace;
                    Error.Visible = true;
                }
            }
        }
    }
}